﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using MySql.Web;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Artemis
{
    public partial class WSpaceQuery : Form
    {
        public string connectionstring = null;
        public Boolean switchdb = false;
        public WSpaceQuery(Boolean Switch)
        {
            switchdb = Switch;
            if (switchdb == true)
            {
                connectionstring = ConfigurationManager.ConnectionStrings["BackupWSpaceDataBase"].ConnectionString.ToString();
            }
            else
            {
                connectionstring = ConfigurationManager.ConnectionStrings["MainWSpaceDataBase"].ConnectionString.ToString();
            }
            InitializeComponent();
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string name = wholename.Text;
                if (name == "K162")
                {
                    richTextBox1.Text = "虫洞彼端的对应出口";
                }
                else
                {

                    string sql = "select * from whole where name=@wholename;";
                    MySqlCommand query = new MySqlCommand(sql);
                    query.Parameters.Add(new MySqlParameter("@wholename", name));
                    DataSet hole = new DataSet();
                    hole = EveCentral.getdata(query, "Wormhole", connectionstring);
                    if (hole != null && hole.Tables[0].Rows.Count != 0)
                    {
                        string overallmass = hole.Tables["Wormhole"].Rows[0]["overallmass"].ToString();
                        string lifecycle = hole.Tables["Wormhole"].Rows[0]["lifecycle"].ToString();
                        string dest = hole.Tables["Wormhole"].Rows[0]["dest"].ToString();
                        string wname = hole.Tables["Wormhole"].Rows[0]["name"].ToString();
                        string maxmass = hole.Tables["Wormhole"].Rows[0]["maxmass"].ToString();
                        string regenerate = hole.Tables["Wormhole"].Rows[0]["regenerate"].ToString();
                        richTextBox1.Text = wname + "\n" + "寿命" + lifecycle + "小时 \n" + "目的地" + dest + " \n" + "总通过质量" + overallmass + "kg \n" + "最大单次通过质量" + maxmass + "kg \n" + "自然恢复质量" + regenerate + "kg/h";
                    }
                    else
                    {
                        richTextBox1.Text = "输入的不是有效虫洞名称！";
                    }
                }
            }
            catch (MySqlException k)
            {
                MessageBox.Show("SQL ERROR " + k);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string wname = wsysname.Text;
                string sql = "select * from wspace where sysName = @wname";
                MySqlCommand query = new MySqlCommand(sql);
                query.Parameters.Add(new MySqlParameter("@wname", wname));
                DataSet wsys = new DataSet();
                wsys = EveCentral.getdata(query, "WSpace", connectionstring);
                if (wsys != null && wsys.Tables[0].Rows.Count != 0)
                {
                    string name = wsys.Tables["WSpace"].Rows[0]["sysName"].ToString();
                    string level = wsys.Tables["WSpace"].Rows[0]["level"].ToString();
                    string phenom = wsys.Tables["WSpace"].Rows[0]["phenomenon"].ToString();
                    string connection = wsys.Tables["WSpace"].Rows[0]["connections"].ToString();
                    char[] param = new char[1];
                    param[0] = ' ';
                    string[] conne = new string[2];
                    conne = connection.Split(param[0]);
                    if (phenom == "")
                    {
                        phenom = "无";
                    }
                    int i = 0;
                    foreach (string tem in conne)
                    {
                        i++;
                    }
                    if (i==1 && conne[0] == "高安" || conne[0] == "低安" || conne[0] == "00")
                    {
                        richTextBox1.Text = name + "\n" + "等级:" + level + "级 \n天象:" + phenom + " \n永联情况:" + conne[0];
                    }
                    if (i==2)
                    {
                        if (conne[1] != null && conne[0] == "高安" || conne[0] == "低安" || conne[0] == "00")
                        {
                            richTextBox1.Text = name + "\n" + "等级:" + level + "级 \n天象:" + phenom + " \n永联情况:" + conne[0] + "和" + conne[1] + "级虫洞";
                        }
                    }
                    if (i==1 && conne[0] == "1" || conne[0] == "2" || conne[0] == "3" || conne[0] == "4" || conne[0] == "5")
                    {
                        richTextBox1.Text = name + "\n" + "等级:" + level + "级 \n天象:" + phenom + " \n永联情况:" + conne[0] + "级虫洞";

                    }
                    if (connection == "")
                    {
                        richTextBox1.Text = richTextBox1.Text = name + "\n" + "等级:" + level + "级 \n天象:" + phenom + " \n永联情况:无";
                    }
                }
                else
                {
                    richTextBox1.Text = "输入的不是有效的虫洞星系编号";
                }

            }
            catch (MySqlException k)
            {
                MessageBox.Show("SQL ERROR" + k);
            }
        }
        
    }
}
